#include "../lvgl/lvgl.h"
#include "iu_manage.h"
#include "lv_obj_class.h"
#include "lv_label.h"
#include <string.h>
#include <stdio.h>  // 新增：文件操作头文件
#include <stdlib.h> // 新增：内存操作头文件

// 外部函数声明
void create_image_slideshow(void);
void create_main_menu(void);

// 新增：全局变量存储当前注册用户信息
UserInfo current_user = {0};

// 手机号绑定弹窗相关变量（保持不变）
static lv_obj_t * phone_dialog = NULL;
static lv_obj_t * phone_ta     = NULL;
static lv_obj_t * phone_kb     = NULL;

static lv_obj_t * pwd_ta         = NULL;
static lv_obj_t * confirm_pwd_ta = NULL;
static lv_obj_t * user_ta        = NULL; // 新增：用户名输入框全局引用

// 购票/登录/注册界面变量（保持不变）
static lv_obj_t * buy_tickets_screen = NULL;
static lv_obj_t * login_screen       = NULL;
static lv_obj_t * register_screen    = NULL;

static bool buy_tickets_screen_hidden = false;
static bool login_screen_hidden       = false;
static bool register_screen_hidden    = false;

// 函数声明（保持不变，新增文件操作函数声明）
static void stop_all_timers(void);
void buy_tickets_screen_init(void);
void create_login(void);
void create_register(void);
void login_button_click_handler(lv_event_t * e);
void register_button_click_handler(lv_event_t * e);
static void back_button_click_handler(lv_event_t * e);
static void exit_button_click_handler(lv_event_t * e);
static void login_ta_event_cb(lv_event_t * e);
static bool validate_user_login(const char * username, const char * password);
static void register_ta_event_cb(lv_event_t * e);
static void phone_ta_event_cb(lv_event_t * e);
static void phone_cancel_event_cb(lv_event_t * e);
static void register_confirm_event_cb(lv_event_t * e);
static void login_confirm_event_cb(lv_event_t * e);
static void register_success_timer_cb(lv_timer_t * t);
static void show_register_error(lv_obj_t * parent, const char * msg);
// 新增：文件操作函数声明
static bool save_user_info(const UserInfo * user);
// 新增：函数声明
static void delete_obj_timer_cb(lv_timer_t * timer);
static void phone_dialog_click_handler(lv_event_t * e); // 添加新函数声明
static void phone_confirm_event_cb(lv_event_t * e);
static bool lv_obj_is_ancestor(lv_obj_t * obj, lv_obj_t * parent);
static void register_success_timer_cb_wrapper(lv_timer_t * timer);

// ------------------------------
// 新增：保存用户信息到user.txt
// ------------------------------
// ------------------------------
// 改进：合并文件检查与写入操作 
// 同时添加fprintf返回值检查
// ------------------------------
static bool lv_obj_is_ancestor(lv_obj_t * obj, lv_obj_t * parent)
{
    if(obj == NULL || parent == NULL) return false;

    lv_obj_t * cur_obj = obj;
    while(cur_obj != NULL) {
        if(cur_obj == parent) {
            return true;
        }
        cur_obj = lv_obj_get_parent(cur_obj);
    }
    return false;
}
// ------------------------------
// 新增：保存用户信息到user.txt
// ------------------------------
static bool save_user_info(const UserInfo * user)
{
    if(!user) {
        LV_LOG_USER("Invalid user info");
        return false;
    }

    // 检查用户名是否已存在
    FILE * file = fopen("user.txt", "r");
    if(file) {
        char line[200];
        bool user_exists = false;

        while(fgets(line, sizeof(line), file)) {
            // 移除行尾的换行符
            line[strcspn(line, "\r\n")] = 0;

            char existing_user[50];
            if(sscanf(line, "%49[^:]:%*s:%*s", existing_user) == 1) {
                if(strcmp(existing_user, user->username) == 0) {
                    user_exists = true;
                    break;
                }
            }
        }
        fclose(file);

        if(user_exists) {
            LV_LOG_USER("Username already exists");
            return false;
        }
    }

    // 以追加模式打开文件写入用户信息
    file = fopen("user.txt", "a");
    if(!file) {
        LV_LOG_USER("Failed to open user.txt for writing");
        return false;
    }

    // 写入用户信息
    fprintf(file, "%s:%s:%s\n", user->username, user->password, user->phone);
    fclose(file);

    LV_LOG_USER("User info saved successfully");
    return true;
}
//---------------------------
// 公共回调：返回按钮（通用）
// ------------------------------

static void back_button_click_handler(lv_event_t * e)
{
    LV_LOG_USER("Back button clicked");
    if(e == NULL) return;

    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // 检查当前是哪个界面并隐藏它
        lv_obj_t * current_screen = lv_scr_act();
        if(current_screen == login_screen) {
            if(login_screen) {
                lv_obj_add_flag(login_screen, LV_OBJ_FLAG_HIDDEN);
                login_screen_hidden = true;
            }
        } else if(current_screen == register_screen) {
            if(register_screen) {
                lv_obj_add_flag(register_screen, LV_OBJ_FLAG_HIDDEN);
                register_screen_hidden = true;
            }
        }

        // 返回购票主界面
        if(buy_tickets_screen) {
            if(buy_tickets_screen_hidden) {
                lv_obj_clear_flag(buy_tickets_screen, LV_OBJ_FLAG_HIDDEN);
                buy_tickets_screen_hidden = false;
            }
        }
        buy_tickets_screen_init();
    }
}

// ------------------------------
// 购票主界面：退出按钮回调
// ------------------------------
static void exit_button_click_handler(lv_event_t * e)
{
    LV_LOG_USER("Exit button clicked");
    if(e == NULL) return;

    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // 隐藏当前界面而不是删除
        if(buy_tickets_screen) {
            lv_obj_add_flag(buy_tickets_screen, LV_OBJ_FLAG_HIDDEN);
            buy_tickets_screen_hidden = true;
        }

        // 跳转到广告页
        create_image_slideshow();
    }
}

// ------------------------------
// 登录界面：事件回调
// ------------------------------
static void login_ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta        = lv_event_get_target(e);
    lv_obj_t * kb        = lv_event_get_user_data(e);

    // 检查对象是否有效
    if(!ta || !kb) return;

    // 查找登录按钮（通过文本框的父对象找到父界面，再查找其中的按钮）
    lv_obj_t * login_btn = NULL;
    lv_obj_t * parent    = lv_obj_get_parent(ta);
    if(parent) {
        // 遍历父对象的子对象，找到登录按钮
        for(int i = 0; i < lv_obj_get_child_cnt(parent); i++) {
            lv_obj_t * child = lv_obj_get_child(parent, i);
            if(child && lv_obj_check_type(child, &lv_btn_class)) {
                lv_obj_t * label = lv_obj_get_child(child, 0);
                if(label && lv_obj_check_type(label, &lv_label_class)) {
                    const char * text = lv_label_get_text(label);
                    if(text && strcmp(text, "登录") == 0) {
                        login_btn = child;
                        break;
                    }
                }
            }
        }
    }

    if(code == LV_EVENT_FOCUSED) {
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);

        // 隐藏登录按钮
        if(login_btn) {
            lv_obj_add_flag(login_btn, LV_OBJ_FLAG_HIDDEN);
        }
    } else if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

        // 显示登录按钮
        if(login_btn) {
            lv_obj_clear_flag(login_btn, LV_OBJ_FLAG_HIDDEN);
        }
    } else if(code == LV_EVENT_READY) {
        // 用户点击了键盘的确认键
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

        // 显示登录按钮
        if(login_btn) {
            lv_obj_clear_flag(login_btn, LV_OBJ_FLAG_HIDDEN);
        }
    }
}
// 新增：验证用户登录信息
static bool validate_user_login(const char * username, const char * password)
{
    if(!username || !password) {
        LV_LOG_USER("Invalid username or password");
        return false;
    }

    FILE * file = fopen("user.txt", "r");
    if(!file) {
        LV_LOG_USER("Failed to open user.txt for reading");
        return false;
    }

    char line[200];
    bool found = false;

    while(fgets(line, sizeof(line), file)) {
        // 移除行尾的换行符
        line[strcspn(line, "\r\n")] = 0;

        char file_username[50], file_password[50], file_phone[20];
        if(sscanf(line, "%49[^:]:%49[^:]:%19s", file_username, file_password, file_phone) == 3) {
            if(strcmp(username, file_username) == 0 && strcmp(password, file_password) == 0) {
                found = true;
                // 保存当前用户信息
                strncpy(current_user.username, file_username, sizeof(current_user.username) - 1);
                current_user.username[sizeof(current_user.username) - 1] = '\0';
                strncpy(current_user.password, file_password, sizeof(current_user.password) - 1);
                current_user.password[sizeof(current_user.password) - 1] = '\0';
                strncpy(current_user.phone, file_phone, sizeof(current_user.phone) - 1);
                current_user.phone[sizeof(current_user.phone) - 1] = '\0';
                break;
            }
        }
    }

    fclose(file);
    return found;
}
// 创建登录界面的键盘和输入框
void create_login_keyboard(lv_obj_t * parent)
{
    // 创建键盘
    lv_obj_t * kb = lv_keyboard_create(parent);
    lv_obj_set_style_bg_opa(kb, LV_OPA_80, LV_PART_MAIN);
    lv_keyboard_set_mode(kb, LV_KEYBOARD_MODE_SPECIAL);
    lv_obj_set_size(kb, LV_PCT(100), LV_PCT(50));
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_text_font(kb, &lv_font_montserrat_24, LV_PART_MAIN);
    // 用户名输入框
    lv_obj_t * user_ta_local = lv_textarea_create(parent); // 修改变量名避免冲突
    lv_obj_align(user_ta_local, LV_ALIGN_TOP_MID, 20, 80);
    lv_obj_set_size(user_ta_local, 300, 50);
    lv_textarea_set_placeholder_text(user_ta_local, "请输入用户名");
    lv_obj_set_style_text_font(user_ta_local, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_add_event_cb(user_ta_local, login_ta_event_cb, LV_EVENT_ALL, kb);
    // 保存引用以供登录验证使用
    user_ta = user_ta_local;

    // 创建标签对象而不是在parent上设置文本
    lv_obj_t * user_label = lv_label_create(parent);
    lv_obj_set_style_text_font(user_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_label_set_text(user_label, "用户名:");
    lv_obj_align_to(user_label, user_ta_local, LV_ALIGN_OUT_LEFT_MID, -10, 0);

    // 密码输入框（隐藏输入内容）
    lv_obj_t * login_pwd_ta = lv_textarea_create(parent);
    lv_obj_align(login_pwd_ta, LV_ALIGN_TOP_MID, 20, 140);
    lv_obj_set_size(login_pwd_ta, 300, 50);
    lv_textarea_set_placeholder_text(login_pwd_ta, "请输入密码");
    lv_textarea_set_password_mode(login_pwd_ta, true); // 密码模式：显示*
    lv_obj_set_style_text_font(login_pwd_ta, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_add_event_cb(login_pwd_ta, login_ta_event_cb, LV_EVENT_ALL, kb);
    // 保存引用以供登录验证使用
    pwd_ta = login_pwd_ta;

    // 创建标签对象而不是在parent上设置文本
    lv_obj_t * pwd_label = lv_label_create(parent);
    lv_obj_set_style_text_font(pwd_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_label_set_text(pwd_label, "密码:");
    lv_obj_align_to(pwd_label, login_pwd_ta, LV_ALIGN_OUT_LEFT_MID, -10, 0);

    // 登录按钮
    lv_obj_t * login_confirm_btn = lv_btn_create(parent);
    lv_obj_set_size(login_confirm_btn, 120, 40);
    lv_obj_align(login_confirm_btn, LV_ALIGN_TOP_MID, 0, 200);
    lv_obj_set_style_bg_color(login_confirm_btn, lv_color_hex(0x007bff), LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(login_confirm_btn, lv_color_hex(0xffffff), LV_STATE_DEFAULT);

    lv_obj_t * login_confirm_label = lv_label_create(login_confirm_btn);
    lv_label_set_text(login_confirm_label, "登录");
    lv_obj_center(login_confirm_label);

    // 添加登录按钮的事件回调
    lv_obj_add_event_cb(login_confirm_btn, login_confirm_event_cb, LV_EVENT_CLICKED, NULL);

    // 将登录按钮存储到文本框的用户数据中，便于事件处理函数访问
    lv_obj_set_user_data(user_ta_local, login_confirm_btn);
    lv_obj_set_user_data(login_pwd_ta, login_confirm_btn);

    // 初始隐藏键盘
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, user_ta_local); // 默认关联用户名框
}

// 登录界面初始化
void create_login(void)
{
    // 如果界面已存在，直接显示
    if(login_screen) {
        if(login_screen_hidden) {
            lv_obj_clear_flag(login_screen, LV_OBJ_FLAG_HIDDEN);
            login_screen_hidden = false;
        }
        lv_scr_load_anim(login_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
        return;
    }

    // 创建登录界面
    login_screen = lv_obj_create(NULL);

    if(!login_screen) {
        LV_LOG_USER("Failed to create login screen");
        return;
    }

    lv_obj_set_size(login_screen, 800, 480);
    lv_obj_set_style_bg_color(login_screen, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(login_screen, LV_OPA_COVER, LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(login_screen);
    if(title) {
        lv_label_set_text(title, "用户登录");
        lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 30);
    }

    // 创建输入框和键盘
    create_login_keyboard(login_screen);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(login_screen);
    if(back_btn) {
        lv_obj_set_size(back_btn, 80, 40);
        lv_obj_align(back_btn, LV_ALIGN_TOP_RIGHT, -20, 20);
        lv_obj_t * back_label = lv_label_create(back_btn);
        if(back_label) {
            lv_label_set_text(back_label, "返回");
            lv_obj_center(back_label);
        }
        lv_obj_add_event_cb(back_btn, back_button_click_handler, LV_EVENT_CLICKED, NULL);
    }

    // 加载界面
    lv_scr_load_anim(login_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
}

// 新增：创建手机号绑定弹窗 完善键盘显示/隐藏逻辑
static void phone_dialog_click_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * dialog    = lv_event_get_target(e);

    if(code == LV_EVENT_CLICKED) {
        // 检查点击的是键盘还是弹窗背景
        lv_obj_t * clicked_obj = lv_event_get_indev(e)->proc.types.pointer.act_obj;

        // 如果点击的是键盘本身或者键盘的子元素，则不处理
        if(clicked_obj == phone_kb || lv_obj_is_ancestor(clicked_obj, phone_kb)) {
            return;
        }

        // 如果键盘当前是隐藏的，显示键盘
        if(lv_obj_has_flag(phone_kb, LV_OBJ_FLAG_HIDDEN)) {
            // 显示键盘
            lv_obj_clear_flag(phone_kb, LV_OBJ_FLAG_HIDDEN);

            // 输入框位置保持不变
            lv_obj_align(phone_ta, LV_ALIGN_TOP_MID, 0, 100);

            // 隐藏确认和取消按钮
            lv_obj_t * confirm_btn = NULL;
            lv_obj_t * cancel_btn  = NULL;

            // 查找确认和取消按钮
            uint32_t child_cnt = lv_obj_get_child_cnt(dialog);
            for(uint32_t i = 0; i < child_cnt; i++) {
                lv_obj_t * child = lv_obj_get_child(dialog, i);
                if(lv_obj_check_type(child, &lv_btn_class)) {
                    lv_obj_t * label = lv_obj_get_child(child, 0);
                    if(label && lv_obj_check_type(label, &lv_label_class)) {
                        const char * text = lv_label_get_text(label);
                        if(strcmp(text, "确认") == 0) {
                            confirm_btn = child;
                        } else if(strcmp(text, "取消") == 0) {
                            cancel_btn = child;
                        }
                    }
                }
            }

            // 隐藏按钮
            if(confirm_btn) lv_obj_add_flag(confirm_btn, LV_OBJ_FLAG_HIDDEN);
            if(cancel_btn) lv_obj_add_flag(cancel_btn, LV_OBJ_FLAG_HIDDEN);

            // 设置焦点到输入框
            lv_obj_add_state(phone_ta, LV_STATE_FOCUSED);
        }
        // 如果键盘当前是显示的，且点击的是弹窗背景部分，则隐藏键盘
        else {
            // 检查点击的是否是弹窗背景（而不是输入框或其他控件）
            if(clicked_obj == dialog || lv_obj_is_ancestor(clicked_obj, dialog)) {
                bool is_background_click = true;

                // 检查点击的是否是输入框、键盘或按钮
                if(clicked_obj == phone_ta || lv_obj_is_ancestor(clicked_obj, phone_ta) || clicked_obj == phone_kb ||
                   lv_obj_is_ancestor(clicked_obj, phone_kb)) {
                    is_background_click = false;
                }

                // 检查是否点击了按钮
                uint32_t child_cnt = lv_obj_get_child_cnt(dialog);
                for(uint32_t i = 0; i < child_cnt; i++) {
                    lv_obj_t * child = lv_obj_get_child(dialog, i);
                    if(lv_obj_check_type(child, &lv_btn_class)) {
                        if(clicked_obj == child || lv_obj_is_ancestor(clicked_obj, child)) {
                            is_background_click = false;
                            break;
                        }
                    }
                }

                // 如果点击的是背景部分，则隐藏键盘
                if(is_background_click) {
                    // 隐藏键盘
                    lv_obj_add_flag(phone_kb, LV_OBJ_FLAG_HIDDEN);

                    // 输入框位置保持不变
                    lv_obj_align(phone_ta, LV_ALIGN_TOP_MID, 0, 100);

                    // 显示确认和取消按钮
                    lv_obj_t * confirm_btn = NULL;
                    lv_obj_t * cancel_btn  = NULL;

                    // 查找确认和取消按钮
                    child_cnt = lv_obj_get_child_cnt(dialog);
                    for(uint32_t i = 0; i < child_cnt; i++) {
                        lv_obj_t * child = lv_obj_get_child(dialog, i);
                        if(lv_obj_check_type(child, &lv_btn_class)) {
                            lv_obj_t * label = lv_obj_get_child(child, 0);
                            if(label && lv_obj_check_type(label, &lv_label_class)) {
                                const char * text = lv_label_get_text(label);
                                if(strcmp(text, "确认") == 0) {
                                    confirm_btn = child;
                                } else if(strcmp(text, "取消") == 0) {
                                    cancel_btn = child;
                                }
                            }
                        }
                    }

                    // 显示按钮
                    if(confirm_btn) lv_obj_clear_flag(confirm_btn, LV_OBJ_FLAG_HIDDEN);
                    if(cancel_btn) lv_obj_clear_flag(cancel_btn, LV_OBJ_FLAG_HIDDEN);

                    // 移除输入框焦点
                    lv_obj_clear_state(phone_ta, LV_STATE_FOCUSED);
                }
            }
        }
    }
}

static void phone_cancel_event_cb(lv_event_t * e)
{
    if(e == NULL || lv_event_get_code(e) != LV_EVENT_CLICKED) return;

    // 关闭手机号弹窗
    if(phone_dialog) {
        lv_obj_add_flag(phone_dialog, LV_OBJ_FLAG_HIDDEN);
    }

    // 显示注册界面（保持注册流程不中断）
    if(register_screen && register_screen_hidden) {
        lv_obj_clear_flag(register_screen, LV_OBJ_FLAG_HIDDEN);
        register_screen_hidden = false;
    }

    // 替换 lv_obj_focus 调用为手动设置焦点
    lv_obj_t * target_ta = confirm_pwd_ta ?: pwd_ta ?: user_ta;
    if(target_ta) {
        // 手动设置焦点到目标文本框
        lv_group_t * group = lv_group_get_default();
        if(group) {
            lv_group_focus_obj(target_ta);
        }
    }
}
// 新增：手机号输入框事件回调（控制键盘显示/隐藏）
static void phone_ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta        = lv_event_get_target(e);
    lv_obj_t * kb        = lv_event_get_user_data(e);

    if(code == LV_EVENT_FOCUSED) {
        // 显示键盘
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);

        // 隐藏确认和取消按钮
        lv_obj_t * dialog      = lv_obj_get_parent(ta);
        lv_obj_t * confirm_btn = NULL;
        lv_obj_t * cancel_btn  = NULL;

        // 查找确认和取消按钮
        uint32_t child_cnt = lv_obj_get_child_cnt(dialog);
        for(uint32_t i = 0; i < child_cnt; i++) {
            lv_obj_t * child = lv_obj_get_child(dialog, i);
            if(lv_obj_check_type(child, &lv_btn_class)) {
                lv_obj_t * label = lv_obj_get_child(child, 0);
                if(label && lv_obj_check_type(label, &lv_label_class)) {
                    const char * text = lv_label_get_text(label);
                    if(strcmp(text, "确认") == 0) {
                        confirm_btn = child;
                    } else if(strcmp(text, "取消") == 0) {
                        cancel_btn = child;
                    }
                }
            }
        }

        // 隐藏按钮
        if(confirm_btn) lv_obj_add_flag(confirm_btn, LV_OBJ_FLAG_HIDDEN);
        if(cancel_btn) lv_obj_add_flag(cancel_btn, LV_OBJ_FLAG_HIDDEN);
    } else if(code == LV_EVENT_DEFOCUSED) {
        // 不再隐藏键盘，保持键盘显示
        // 点击输入框时也不再隐藏键盘，而是保持显示
        return;
    } else if(code == LV_EVENT_READY) {
        // 用户点击了键盘的确认键
        // 隐藏键盘
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

        // 显示确认和取消按钮
        lv_obj_t * dialog      = lv_obj_get_parent(ta);
        lv_obj_t * confirm_btn = NULL;
        lv_obj_t * cancel_btn  = NULL;

        // 查找确认和取消按钮
        uint32_t child_cnt = lv_obj_get_child_cnt(dialog);
        for(uint32_t i = 0; i < child_cnt; i++) {
            lv_obj_t * child = lv_obj_get_child(dialog, i);
            if(lv_obj_check_type(child, &lv_btn_class)) {
                lv_obj_t * label = lv_obj_get_child(child, 0);
                if(label && lv_obj_check_type(label, &lv_label_class)) {
                    const char * text = lv_label_get_text(label);
                    if(strcmp(text, "确认") == 0) {
                        confirm_btn = child;
                    } else if(strcmp(text, "取消") == 0) {
                        cancel_btn = child;
                    }
                }
            }
        }

        // 显示按钮
        if(confirm_btn) lv_obj_clear_flag(confirm_btn, LV_OBJ_FLAG_HIDDEN);
        if(cancel_btn) lv_obj_clear_flag(cancel_btn, LV_OBJ_FLAG_HIDDEN);
    }
}

// 新增：定时器回调函数，用于注册成功后跳转到登录界面
// ... existing code ...
static void register_success_timer_cb(lv_timer_t * t)
{
    lv_obj_t * msgbox = (lv_obj_t *)t->user_data;

    // 删除消息框
    if(msgbox) {
        lv_msgbox_close(msgbox);
    }

    // 隐藏注册界面
    if(register_screen) {
        lv_obj_add_flag(register_screen, LV_OBJ_FLAG_HIDDEN);
        register_screen_hidden = true;
    }

    // 延迟创建登录界面，确保对象状态稳定
    lv_timer_t * timer = lv_timer_create(register_success_timer_cb_wrapper, 100, NULL);
    lv_timer_set_repeat_count(timer, 1);

    // 删除当前定时器
    lv_timer_del(t);
}
// ... existing code ...
// 添加消息框事件回调函数
// ... existing code ...
static void success_msgbox_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * msgbox    = lv_event_get_current_target(e);

    if(code == LV_EVENT_VALUE_CHANGED) {
        // 用户点击了确定按钮
        const char * txt = lv_msgbox_get_active_btn_text(msgbox);
        if(txt && strcmp(txt, "确定") == 0) {
            // 关闭消息框
            lv_msgbox_close(msgbox);

            // 隐藏注册界面而不是设置为NULL
            if(register_screen) {
                lv_obj_add_flag(register_screen, LV_OBJ_FLAG_HIDDEN);
                register_screen_hidden = true;
            }

            // 延迟创建登录界面，确保消息框完全关闭
            lv_timer_t * timer = lv_timer_create(register_success_timer_cb_wrapper, 100, NULL);
            lv_timer_set_repeat_count(timer, 1);
        }
    }
}
// ... existing code ...

static void phone_confirm_event_cb(lv_event_t * e)
{
    // 获取手机号输入框中的文本
    const char * phone = lv_textarea_get_text(phone_ta);

    // 合并校验：首位为1且长度为11位
    if(strlen(phone) != 11 || phone[0] != '1') {
        LV_LOG_USER("Invalid phone number (must start with 1 and 11 digits)");

        // 删除旧的错误提示标签
        lv_obj_t * old_err_label = NULL;
        uint32_t i;
        for(i = 0; i < lv_obj_get_child_cnt(phone_dialog); i++) {
            lv_obj_t * child = lv_obj_get_child(phone_dialog, i);
            if(lv_obj_check_type(child, &lv_label_class)) { // 使用 lv_obj_check_type 替代 lv_obj_is_type
                const char * txt = lv_label_get_text(child);
                if(strcmp(txt, "手机号格式错误") == 0) {
                    old_err_label = child;
                    break;
                }
            }
        }
        if(old_err_label) {
            lv_obj_del(old_err_label);
        }

        // 创建新的错误提示
        lv_obj_t * err_label = lv_label_create(phone_dialog);
        lv_label_set_text(err_label, "手机号格式错误");
        lv_obj_set_style_text_color(err_label, lv_color_hex(0xdc3545), 0);
        lv_obj_align(err_label, LV_ALIGN_TOP_MID, 0, 140);

        // 创建一个适配 lv_timer_create 的回调函数
        lv_timer_t * timer = lv_timer_create(delete_obj_timer_cb, 3000, err_label);
        lv_timer_set_repeat_count(timer, 1);

        return;
    }

    // 收集注册信息到current_user
    const char * username = lv_textarea_get_text(user_ta);
    const char * password = lv_textarea_get_text(pwd_ta);

    // 校验用户名和密码不为空
    if(strlen(username) == 0 || strlen(password) == 0) {
        LV_LOG_USER("Username or password is empty");
        // 显示错误提示（可参考原有错误提示逻辑）
        return;
    }

    if(strlen(username) < 2) {
        LV_LOG_USER("Username too short"); // 显示"用户名长度至少2位"的错误提示（参考现有逻辑）
        return;
    }
    if(strlen(password) < 2) {
        LV_LOG_USER("password too short"); // 显示"密码长度至少6位"的错误提示（参考现有逻辑）
        return;
    }

    // 填充用户信息结构体
    strncpy(current_user.username, username, sizeof(current_user.username) - 1);
    current_user.username[sizeof(current_user.username) - 1] = '\0'; // 新增

    strncpy(current_user.password, password, sizeof(current_user.password) - 1);
    current_user.password[sizeof(current_user.password) - 1] = '\0'; // 新增

    strncpy(current_user.phone, phone, sizeof(current_user.phone) - 1);
    current_user.phone[sizeof(current_user.phone) - 1] = '\0'; // 新增

    // 保存用户信息到文件
    if(!save_user_info(&current_user)) {

        // 用户名已存在，显示提示
        lv_obj_t * old_err_label = NULL;
        uint32_t i;
        for(i = 0; i < lv_obj_get_child_cnt(phone_dialog); i++) {
            lv_obj_t * child = lv_obj_get_child(phone_dialog, i);
            if(lv_obj_check_type(child, &lv_label_class)) {
                const char * txt = lv_label_get_text(child);
                if(strcmp(txt, "用户名已存在") == 0) {
                    old_err_label = child;
                    break;
                }
            }
        }
        if(old_err_label) {
            lv_obj_del(old_err_label);
        }

        lv_obj_t * err_label = lv_label_create(phone_dialog);
        lv_label_set_text(err_label, "用户名已存在");
        lv_obj_set_style_text_color(err_label, lv_color_hex(0xdc3545), 0);
        lv_obj_align(err_label, LV_ALIGN_TOP_MID, 0, 140);
        lv_timer_create(delete_obj_timer_cb, 3000, err_label); // 新增：3秒后自动删除

        return;
    }

    // 保存成功，关闭弹窗并跳转登录
    if(phone_dialog) {
        lv_obj_add_flag(phone_dialog, LV_OBJ_FLAG_HIDDEN);
    }

    if(register_screen) {
        lv_obj_add_flag(register_screen, LV_OBJ_FLAG_HIDDEN);
        register_screen_hidden = true;
    }

    // 显示注册成功提示
    static const char * btn_txts[] = {"确定", ""};
    lv_obj_t * success_msg         = lv_msgbox_create(NULL, "成功", "注册成功，请登录", btn_txts, false);
    lv_obj_center(success_msg);

    // 添加事件回调处理用户点击确定按钮
    lv_obj_add_event_cb(success_msg, success_msgbox_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    // 使用适配 lv_timer_create 的回调函数
    lv_timer_t * timer = lv_timer_create(register_success_timer_cb, 1500, success_msg);
    lv_timer_set_repeat_count(timer, 1);
}
// ------------------------------
// 注册界面：事件回调
// ------------------------------
static void register_ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta        = lv_event_get_target(e);
    lv_obj_t * kb        = lv_event_get_user_data(e);

    // 检查对象是否有效
    if(!ta || !kb) return;

    lv_obj_t * register_btn = lv_obj_get_user_data(ta); // 获取注册按钮引用

    if(code == LV_EVENT_FOCUSED) {
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
        // 当键盘显示时，隐藏注册按钮
        if(register_btn) {
            lv_obj_add_flag(register_btn, LV_OBJ_FLAG_HIDDEN);
        }
    } else if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        // 当键盘隐藏时，显示注册按钮
        if(register_btn) {
            lv_obj_clear_flag(register_btn, LV_OBJ_FLAG_HIDDEN);
        }
    } else if(code == LV_EVENT_READY) {
        // 用户点击了键盘的确认键
        // 隐藏键盘
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

        // 显示注册按钮
        if(register_btn) {
            lv_obj_clear_flag(register_btn, LV_OBJ_FLAG_HIDDEN);
        }

        // 根据当前输入框决定下一步操作
        if(ta == user_ta) {
            // 若当前是用户名输入框，则聚焦到密码框
            if(pwd_ta) lv_obj_add_state(pwd_ta, LV_STATE_FOCUSED);
        } else if(ta == pwd_ta) {
            // 若当前是密码输入框，则聚焦到确认密码框
            if(confirm_pwd_ta) lv_obj_add_state(confirm_pwd_ta, LV_STATE_FOCUSED);
        } else if(ta == confirm_pwd_ta) {
            // 若当前是确认密码框，保持焦点状态但隐藏键盘
            lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        }
    }
}

// 创建注册界面的键盘和输入框
static void create_register_keyboard(lv_obj_t * parent)
{
    // 创建键盘
    lv_obj_t * kb = lv_keyboard_create(parent);
    if(!kb) return;

    lv_obj_set_style_bg_opa(kb, LV_OPA_80, 0);
    lv_keyboard_set_mode(kb, LV_KEYBOARD_MODE_SPECIAL);
    lv_obj_set_size(kb, LV_PCT(100), LV_PCT(50));
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_text_font(kb, &lv_font_montserrat_24, LV_PART_MAIN);

    // 用户名输入框
    user_ta = lv_textarea_create(parent);
    if(!user_ta) return;

    lv_obj_align(user_ta, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_set_size(user_ta, 300, 50);
    lv_textarea_set_placeholder_text(user_ta, "请设置用户名");
    lv_obj_set_style_text_font(user_ta, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_add_event_cb(user_ta, register_ta_event_cb, LV_EVENT_ALL, kb);
    lv_textarea_set_accepted_chars(user_ta, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
    lv_textarea_set_max_length(user_ta, 19);

    lv_obj_t * user_label = lv_label_create(parent);
    if(user_label) {
        lv_label_set_text(user_label, "用户名:");
        lv_obj_align_to(user_label, user_ta, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    }

    // 密码输入框
    pwd_ta = lv_textarea_create(parent);
    if(!pwd_ta) return;

    lv_obj_align(pwd_ta, LV_ALIGN_TOP_MID, 0, 120);
    lv_obj_set_size(pwd_ta, 300, 50);
    lv_textarea_set_placeholder_text(pwd_ta, "请设置密码");
    lv_textarea_set_password_mode(pwd_ta, true);
    lv_obj_set_style_text_font(pwd_ta, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_add_event_cb(pwd_ta, register_ta_event_cb, LV_EVENT_ALL, kb);
    lv_textarea_set_accepted_chars(pwd_ta, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
    lv_textarea_set_max_length(pwd_ta, 19);

    lv_obj_t * pwd_label = lv_label_create(parent);
    if(pwd_label) {
        lv_label_set_text(pwd_label, "密码:");
        lv_obj_align_to(pwd_label, pwd_ta, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    }

    // 确认密码输入框
    confirm_pwd_ta = lv_textarea_create(parent);
    if(!confirm_pwd_ta) return;

    lv_obj_align(confirm_pwd_ta, LV_ALIGN_TOP_MID, 0, 180);
    lv_obj_set_size(confirm_pwd_ta, 300, 50);
    lv_textarea_set_placeholder_text(confirm_pwd_ta, "请确认密码");
    lv_textarea_set_password_mode(confirm_pwd_ta, true);
    lv_obj_set_style_text_font(confirm_pwd_ta, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_add_event_cb(confirm_pwd_ta, register_ta_event_cb, LV_EVENT_ALL, kb);

    lv_obj_t * confirm_pwd_label = lv_label_create(parent);
    if(confirm_pwd_label) {
        lv_label_set_text(confirm_pwd_label, "确认密码:");
        lv_obj_align_to(confirm_pwd_label, confirm_pwd_ta, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    }

    // 注册按钮
    lv_obj_t * register_confirm_btn = lv_btn_create(parent);
    if(!register_confirm_btn) return;

    lv_obj_set_size(register_confirm_btn, 120, 40);
    lv_obj_align(register_confirm_btn, LV_ALIGN_TOP_MID, 0, 260);
    lv_obj_set_style_bg_color(register_confirm_btn, lv_color_hex(0x28a745), LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(register_confirm_btn, lv_color_hex(0xffffff), LV_STATE_DEFAULT);

    lv_obj_t * register_confirm_label = lv_label_create(register_confirm_btn);
    if(register_confirm_label) {
        lv_label_set_text(register_confirm_label, "注册");
        lv_obj_center(register_confirm_label);
    }

    // 添加注册按钮的事件回调
    lv_obj_add_event_cb(register_confirm_btn, register_confirm_event_cb, LV_EVENT_CLICKED, NULL);

    // 将注册按钮和键盘存储到文本框的用户数据中
    lv_obj_set_user_data(user_ta, register_confirm_btn);
    lv_obj_set_user_data(pwd_ta, register_confirm_btn);
    lv_obj_set_user_data(confirm_pwd_ta, register_confirm_btn);

    // 初始隐藏键盘
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    lv_keyboard_set_textarea(kb, user_ta); // 默认关联用户名框
}

// 新增：从user.txt文件中获取用户手机号
static bool get_user_phone(const char * username, char * phone, size_t phone_size)
{
    if(!username || !phone || phone_size < 12) return false;

    FILE * file = fopen("user.txt", "r");
    if(!file) {
        LV_LOG_USER("Failed to open user.txt for reading");
        return false;
    }

    char line[200];
    bool found = false;

    while(fgets(line, sizeof(line), file)) {
        // 移除行尾的换行符
        line[strcspn(line, "\r\n")] = 0;

        char file_username[50], file_password[50], file_phone[20];
        if(sscanf(line, "%49[^:]:%49[^:]:%19s", file_username, file_password, file_phone) == 3) {
            if(strcmp(username, file_username) == 0) {
                strncpy(phone, file_phone, phone_size - 1);
                phone[phone_size - 1] = '\0';
                found                 = true;
                break;
            }
        }
    }

    fclose(file);
    return found;
}

// 添加确认按钮的回调函数
static void login_confirm_event_cb(lv_event_t * e)
{
    LV_LOG_USER("Login confirm button clicked");
    if(e == NULL) return;

    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // 获取输入的用户名和密码
        const char * username = lv_textarea_get_text(user_ta);
        const char * password = lv_textarea_get_text(pwd_ta);

        // 验证登录信息
        if(validate_user_login(username, password)) {
            LV_LOG_USER("Login successful");

            // 登录成功，跳转到主菜单
            if(login_screen) {
                lv_obj_add_flag(login_screen, LV_OBJ_FLAG_HIDDEN);
                login_screen_hidden = true;
            }
            create_main_menu();
        } else {
            LV_LOG_USER("Invalid username or password");
            // 显示错误信息
            lv_obj_t * err_label = lv_label_create(login_screen);
            lv_label_set_text(err_label, "用户名或密码错误");
            lv_obj_set_style_text_color(err_label, lv_color_hex(0xdc3545), 0);
            lv_obj_align(err_label, LV_ALIGN_TOP_MID, 0, 250);

            // 使用适配 lv_timer_create 的回调函数
            lv_timer_t * timer = lv_timer_create(delete_obj_timer_cb, 3000, err_label);
            lv_timer_set_repeat_count(timer, 1);
        }
    }
}
// 在合适的位置添加用于删除对象的定时器回调函数
static void delete_obj_timer_cb(lv_timer_t * timer)
{
    lv_obj_t * obj = (lv_obj_t *)timer->user_data;
    if(obj) {
        lv_obj_del(obj);
    }
    lv_timer_del(timer);
}
// 添加用于适配 register_success_timer_cb 的包装函数

// 添加用于适配 register_success_timer_cb 的包装函数

static void register_success_timer_cb_wrapper(lv_timer_t * timer)
{
    create_login();
    lv_timer_del(timer); // 删除定时器自身
}

// 新增：创建手机号绑定弹窗
static void create_phone_dialog(void)
{
    // 如果弹窗已存在则先删除
    if(phone_dialog) {
        lv_obj_del(phone_dialog);
        phone_dialog = NULL;
    }

    // 创建弹窗（模态框）
    phone_dialog = lv_obj_create(lv_scr_act());
    lv_obj_set_size(phone_dialog, 480, 380);
    lv_obj_align(phone_dialog, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(phone_dialog, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_width(phone_dialog, 2, 0);
    lv_obj_set_style_border_color(phone_dialog, lv_color_hex(0x007bff), 0);
    lv_obj_set_style_radius(phone_dialog, 5, 0);

    // 弹窗标题
    lv_obj_t * title = lv_label_create(phone_dialog);
    lv_label_set_text(title, "绑定手机号");
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16_all, 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);

    // 提示文本
    lv_obj_t * hint = lv_label_create(phone_dialog);
    lv_label_set_text(hint, "请输入11位手机号码完成绑定");
    lv_obj_set_style_text_color(hint, lv_color_hex(0x666666), 0);
    lv_obj_align(hint, LV_ALIGN_TOP_MID, 0, 50);

    // 手机号输入框
    phone_ta = lv_textarea_create(phone_dialog);
    lv_textarea_set_max_length(phone_ta, 11);
    lv_textarea_set_one_line(phone_ta, true);
    lv_textarea_set_accepted_chars(phone_ta, "0123456789");
    lv_obj_set_size(phone_ta, 200, 40);
    lv_obj_align(phone_ta, LV_ALIGN_TOP_MID, 0, 100);
    lv_textarea_set_placeholder_text(phone_ta, "请输入手机号");
    lv_obj_set_style_text_font(phone_ta, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
    lv_obj_clear_flag(phone_ta, LV_OBJ_FLAG_SCROLLABLE); // 取消滚动

    // 创建数字键盘（使用带功能键的数字键盘模式）
    phone_kb = lv_keyboard_create(phone_dialog);
    lv_obj_set_size(phone_kb, LV_PCT(100), 190); // 380的一半是190
    lv_obj_align(phone_kb, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_keyboard_set_mode(phone_kb, LV_KEYBOARD_MODE_NUMBER); // 保持数字键盘模式
    lv_obj_set_style_bg_opa(phone_kb, LV_OPA_80, 0);
    lv_obj_set_style_text_font(phone_kb, &lv_font_montserrat_24, LV_PART_MAIN);

    // 默认隐藏键盘
    lv_obj_add_flag(phone_kb, LV_OBJ_FLAG_HIDDEN);

    // 绑定输入框和键盘事件
    lv_obj_add_event_cb(phone_ta, phone_ta_event_cb, LV_EVENT_ALL, phone_kb);
    lv_keyboard_set_textarea(phone_kb, phone_ta);

    // 确认按钮（默认隐藏）
    lv_obj_t * confirm_btn = lv_btn_create(phone_dialog);
    lv_obj_set_size(confirm_btn, 100, 40);
    lv_obj_align(confirm_btn, LV_ALIGN_BOTTOM_MID, -60, -30);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x28a745), 0);
    lv_obj_set_style_text_color(confirm_btn, lv_color_hex(0xffffff), 0);
    lv_obj_t * confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, "确认");
    lv_obj_center(confirm_label);
    lv_obj_add_event_cb(confirm_btn, phone_confirm_event_cb, LV_EVENT_CLICKED, NULL);
    // 默认隐藏确认按钮
    lv_obj_add_flag(confirm_btn, LV_OBJ_FLAG_HIDDEN);

    // 取消按钮（默认隐藏）
    lv_obj_t * cancel_btn = lv_btn_create(phone_dialog);
    lv_obj_set_size(cancel_btn, 100, 40);
    lv_obj_align(cancel_btn, LV_ALIGN_BOTTOM_MID, 60, -30);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(0xdc3545), LV_PART_MAIN);
    lv_obj_set_style_text_color(cancel_btn, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_t * cancel_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_label, "取消");
    lv_obj_center(cancel_label);
    lv_obj_add_event_cb(cancel_btn, phone_cancel_event_cb, LV_EVENT_CLICKED, NULL);
    // 默认隐藏取消按钮
    lv_obj_add_flag(cancel_btn, LV_OBJ_FLAG_HIDDEN);

    // 添加弹窗点击事件，用于显示键盘和处理背景点击隐藏键盘
    lv_obj_add_event_cb(phone_dialog, phone_dialog_click_handler, LV_EVENT_CLICKED, NULL);

    // 显示弹窗
    lv_obj_clear_flag(phone_dialog, LV_OBJ_FLAG_HIDDEN);
}
// 添加弹窗点击事件处理函数 完善键盘显示/隐藏逻辑
// 新增错误提示函数（复用逻辑）
static void show_register_error(lv_obj_t * parent, const char * msg)
{
    uint32_t i;
    for(i = 0; i < lv_obj_get_child_cnt(parent); i++) {
        lv_obj_t * child = lv_obj_get_child(parent, i);
        if(lv_obj_check_type(child, &lv_label_class)) { // 使用 lv_obj_check_type 替代 lv_obj_is_type
            const char * txt = lv_label_get_text(child);
            if(strcmp(txt, "密码长度至少6位") == 0 || strcmp(txt, "两次密码输入不一致") == 0) {
                lv_obj_del(child);
                break;
            }
        }
    }
    // 如果msg为空则只删除错误提示，不创建新的
    if(msg[0] == '\0') return;

    // 创建新提示
    lv_obj_t * err_label = lv_label_create(parent);
    lv_label_set_text(err_label, msg);
    lv_obj_set_style_text_color(err_label, lv_color_hex(0xdc3545), 0);
    lv_obj_align(err_label, LV_ALIGN_TOP_MID, 0, 310); // 注册按钮下方
}
// 在register_confirm_event_cb中调用
static void register_confirm_event_cb(lv_event_t * e)
{
    LV_LOG_USER("Register confirm button clicked");
    if(e == NULL) return;

    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // 从全局输入框获取密码文本
        const char * pwd         = lv_textarea_get_text(pwd_ta);
        const char * confirm_pwd = lv_textarea_get_text(confirm_pwd_ta);

        // 密码校验逻辑
        if(strlen(pwd) < 6) {
            LV_LOG_USER("Password too short");
            show_register_error(register_screen, "密码长度至少6位");
            return;
        }
        if(strcmp(pwd, confirm_pwd) != 0) {
            LV_LOG_USER("Passwords do not match");
            show_register_error(register_screen, "两次密码输入不一致");
            return;
        }

        // 校验通过，显示手机号绑定弹窗
        create_phone_dialog();
    }
}
// 注册界面初始化
void create_register(void)
{
    // 如果界面已存在，直接显示
    if(register_screen) {
        if(register_screen_hidden) {
            lv_obj_clear_flag(register_screen, LV_OBJ_FLAG_HIDDEN);
            register_screen_hidden = false;
        }
        // 显示界面时清理错误提示
        show_register_error(register_screen, ""); // 传入空字符串触发删除逻辑

        lv_scr_load_anim(register_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
        return;
    }

    // 创建注册界面
    register_screen = lv_obj_create(NULL);

    if(!register_screen) {
        LV_LOG_USER("Failed to create register screen");
        return;
    }

    lv_obj_set_size(register_screen, 800, 480);
    lv_obj_set_style_bg_color(register_screen, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(register_screen, LV_OPA_COVER, LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(register_screen);
    if(title) {
        lv_label_set_text(title, "用户注册");
        lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 30);
    }

    // 创建输入框和键盘
    create_register_keyboard(register_screen);

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(register_screen);
    if(back_btn) {
        lv_obj_set_size(back_btn, 80, 40);
        lv_obj_align(back_btn, LV_ALIGN_TOP_RIGHT, -20, 20);
        lv_obj_t * back_label = lv_label_create(back_btn);
        if(back_label) {
            lv_label_set_text(back_label, "返回");
            lv_obj_center(back_label);
        }
        lv_obj_add_event_cb(back_btn, back_button_click_handler, LV_EVENT_CLICKED, NULL);
    }

    // 加载界面
    lv_scr_load_anim(register_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
}

// ------------------------------
// 购票主界面初始化
// ------------------------------
void buy_tickets_screen_init(void)
{
    // 如果界面已存在，直接显示
    if(buy_tickets_screen) {
        if(buy_tickets_screen_hidden) {
            lv_obj_clear_flag(buy_tickets_screen, LV_OBJ_FLAG_HIDDEN);
            buy_tickets_screen_hidden = false;
        }
        lv_scr_load_anim(buy_tickets_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
        return;
    }

    // 创建主界面
    buy_tickets_screen = lv_obj_create(NULL);

    if(!buy_tickets_screen) {
        LV_LOG_USER("Failed to create buy tickets screen");
        return;
    }

    lv_obj_set_size(buy_tickets_screen, 800, 480);
    lv_obj_set_style_bg_color(buy_tickets_screen, lv_color_hex(0xffffff), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(buy_tickets_screen, LV_OPA_COVER, LV_PART_MAIN);

    // 标题
    lv_obj_t * title = lv_label_create(buy_tickets_screen);
    if(title) {
        lv_label_set_text(title, "电影购票-请先登录账号");
        lv_obj_set_style_text_font(title, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
        lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 30);
    }

    // 登录按钮
    lv_obj_t * login_btn = lv_btn_create(buy_tickets_screen);
    if(login_btn) {
        lv_obj_set_size(login_btn, 200, 60);
        lv_obj_align(login_btn, LV_ALIGN_TOP_MID, 0, 100);
        lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x007bff), LV_STATE_DEFAULT);
        lv_obj_set_style_text_color(login_btn, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
        lv_obj_set_style_radius(login_btn, 5, 0);

        lv_obj_t * login_label = lv_label_create(login_btn);
        if(login_label) {
            lv_obj_set_style_text_font(login_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
            lv_label_set_text(login_label, "登录");
            lv_obj_center(login_label);
        }
        lv_obj_add_event_cb(login_btn, login_button_click_handler, LV_EVENT_CLICKED, NULL);
    }

    // 注册按钮
    lv_obj_t * register_btn = lv_btn_create(buy_tickets_screen);
    if(register_btn) {
        lv_obj_set_size(register_btn, 200, 60);
        lv_obj_align(register_btn, LV_ALIGN_TOP_MID, 0, 180);
        lv_obj_set_style_bg_color(register_btn, lv_color_hex(0x28a745), LV_STATE_DEFAULT);
        lv_obj_set_style_text_color(register_btn, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
        lv_obj_set_style_radius(register_btn, 5, 0);

        lv_obj_t * register_label = lv_label_create(register_btn);
        if(register_label) {
            lv_obj_set_style_text_font(register_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
            lv_label_set_text(register_label, "注册");
            lv_obj_center(register_label);
        }
        lv_obj_add_event_cb(register_btn, register_button_click_handler, LV_EVENT_CLICKED, NULL);
    }

    // 退出按钮
    lv_obj_t * exit_btn = lv_btn_create(buy_tickets_screen);
    if(exit_btn) {
        lv_obj_set_size(exit_btn, 200, 60);
        lv_obj_align(exit_btn, LV_ALIGN_TOP_MID, 0, 260);
        lv_obj_set_style_bg_color(exit_btn, lv_color_hex(0xdc3545), LV_STATE_DEFAULT);
        lv_obj_set_style_text_color(exit_btn, lv_color_hex(0xffffff), LV_STATE_DEFAULT);
        lv_obj_set_style_radius(exit_btn, 5, 0);

        lv_obj_t * exit_label = lv_label_create(exit_btn);
        if(exit_label) {
            lv_obj_set_style_text_font(exit_label, &SourceHanSansCN_Light_2_16_all, LV_PART_MAIN);
            lv_label_set_text(exit_label, "退出");
            lv_obj_center(exit_label);
        }
        lv_obj_add_event_cb(exit_btn, exit_button_click_handler, LV_EVENT_CLICKED, NULL);
    }

    // 加载界面
    lv_scr_load_anim(buy_tickets_screen, LV_SCR_LOAD_ANIM_FADE_ON, 300, 0, false);
}

// 登录按钮点击回调（跳转到登录界面）
void login_button_click_handler(lv_event_t * e)
{
    if(e == NULL || lv_event_get_code(e) != LV_EVENT_CLICKED) return;
    LV_LOG_USER("Login button clicked");

    // 隐藏当前界面
    if(buy_tickets_screen) {
        lv_obj_add_flag(buy_tickets_screen, LV_OBJ_FLAG_HIDDEN);
        buy_tickets_screen_hidden = true;
    }

    create_login();
}

// 注册按钮点击回调（跳转到注册界面）
void register_button_click_handler(lv_event_t * e)
{
    if(e == NULL || lv_event_get_code(e) != LV_EVENT_CLICKED) return;
    LV_LOG_USER("Register button clicked");

    // 隐藏当前界面
    if(buy_tickets_screen) {
        lv_obj_add_flag(buy_tickets_screen, LV_OBJ_FLAG_HIDDEN);
        buy_tickets_screen_hidden = true;
    }

    create_register();
}

// 停止所有定时器的函数
static void stop_all_timers(void)
{
    // 空函数，保留以避免编译错误，但不执行任何操作
    // 因为当前购票界面没有使用定时器
}
// 在适当的地方添加对象清理代码
static void cleanup_register_screen(void)
{
    if(register_screen) {
        lv_obj_del(register_screen);
        register_screen        = NULL;
        register_screen_hidden = false;
    }
}